Ext.define('CS.panel.Tree', {
	extend: 'Ext.tree.Panel',
	requires: ['CS.data.TreeStore'],

	rootVisible: false,

	useArrows: true,

	constructor: function(config) {
		var me = this;

		config = CS.apply({}, config);

		config.listeners = config.listeners || {};

		if (config.needCheck) {
			config.listeners.checkchange = function(node, checked) {
				if (window.event.ctrlKey) {
					node.cascadeBy(function(child) {
						child.set('checked', checked);
					});
				}

				if (!checked) {
					node.bubble(function(parentNode) {
						parentNode.set('checked', false);
					});
				}
			};
		}

		config.isLoad = CS.getBoolean(config.isLoad, true);

		var storeConfig = {
			url: config.url,
			isAsync: config.isAsync,
			autoLoad: config.isLoad,
			needCheck: config.needCheck
		};

		if (config.fields) {
			storeConfig.fields = config.fields.split(',');
		}

		config.store = Ext.create('CS.data.TreeStore', storeConfig);

		me.callParent([config]);
	},

	clear: function() {
		var root = this.getRootNode();

		if (root.childNodes) {
			root.removeAll();
		}
	},

	reload: function(params, callbackFn) {
		var me = this;

		me.store.reload({
			params: CS.apply(params || {}, me.extendParam),
			callback: callbackFn
		});
	},

	getSelection: function(notNeedCls) {
		var me = this, clsName = me.clsName, data = [];

		me.getRootNode().cascadeBy(function(child) {
			if (child.get('checked')) {
				data.push(child.data);
			}
		});

		return CS.getPackageData(data, clsName, notNeedCls);
	}

});
